# -*- coding: utf-8 -*-
"""
Created on Thu Nov  2 09:20:12 2017

@author: xuanlei
"""

import numpy as np
import pandas as pd
from sklearn.utils import shuffle  
from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import sklearn.preprocessing as prep
import xgboost as xgb
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDAs
from sklearn.ensemble import GradientBoostingClassifier


def load_data():
    
    globals()['loan_train'] = pd.read_csv('E:\\c\\train_v2.csv',skip_blank_lines = True,encoding = 'utf8')
    globals()['loan_test'] = pd.read_csv('E:\\c\\test_v2.csv',skip_blank_lines = True,encoding = 'utf8')

    
    


def standard_scale(X_train, X_test):
    preprocessor = prep.StandardScaler().fit(X_train)
    X_train = preprocessor.transform(X_train)
    X_test = preprocessor.transform(X_test)
    return X_train, X_test




def get_xy(df):
    df = df.fillna(method='pad')
    df = df.fillna(method='bfill')
#    df = df.fillna(df.mean())
    df = df.replace('NA',0)
    x = df.drop(['loss','label'],axis=1)
    x = x.astype('float')
    y = df['label']
    return x,y

def get_x(df):
    df = df.fillna(method='pad')
    df = df.fillna(method='bfill')
#    df = df.fillna(df.mean())
    df = df.replace('NA',0)
    df = df.astype('float')
    return df

def classification_report(y_true, y_pred):  
    from sklearn.metrics import classification_report  
    print ("classification_report(left: labels):")  
    print (classification_report(y_true, y_pred))  
    
def confusion_matrix_plot_matplotlib(y_truth, y_predict,cmap=plt.cm.Blues):
    """Matplotlib绘制混淆矩阵图
    parameters   cmap=plt.cm.Blues
    ----------
        y_truth: 真实的y的值, 1d array
        y_predict: 预测的y的值, 1d array
        cmap: 画混淆矩阵图的配色风格, 使用cm.Blues
    """
    cm = confusion_matrix(y_truth, y_predict)
    plt.matshow(cm, cmap=cmap)  # 混淆矩阵图
    plt.colorbar()  # 颜色标签
 
    for x in range(len(cm)):  # 数据标签
        for y in range(len(cm)):
            plt.annotate(cm[x, y], xy=(x, y), horizontalalignment='center', verticalalignment='center')
 
    plt.ylabel('True label')  # 坐标轴标签
    plt.xlabel('Predicted label')  # 坐标轴标签
    plt.show()  # 显示作图结果
    
def regroup(df):
    df.loc[df['loss']!=0,'label']=1
    df.loc[df['loss']==0,'label']=0
    return df
    
